#######################################################################################
### Script file to reproduce the figure in Chapter 1
###
### Created: 2-23-23
### Modified: 
###
#######################################################################################

# Load libraries
library(ggplot2)
library(ggthemes)

# Replications of Figure 1: Number of Studies Using the Same Country-Year Observations
# (Keeping Only Articles Published After 2010, and Data Covering European Countries from
# 1969 to 2009)

################################################################################
### Figure 1: Number of studies using the same country-year observations
### (keeping only articles published after 2010, and data covering European countries from 1969-2009)
################################################################################

# loading data
literature_sample <- readRDS("~/Dropbox/partyBrands/elementsText/Replication/Chapter 1/fig1_literature_sample.RDS")

# export the plot as a PDF file
pdf("~/Downloads/HeatMap_2009.pdf", width = 8, height = 6)

ggplot(literature_sample, aes(x = years, y = reorder(country, +total_observations), fill = observations)) +
  geom_tile(color = "white") +
  scale_fill_gradient(low = "#FFE6E6", high = "#FF0000", breaks = seq(0, 9, 2), labels = seq(0, 9, 2)) +
  theme_bw() +
  xlab("Year") +
  ylab("Country") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        axis.title.x = element_text(margin = margin(t = 10)),
        axis.title.y = element_text(margin = margin(r = 10)),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "bottom",
        plot.title = element_text(hjust = 0.5)) +
  geom_text(aes(label = ifelse(observations > 0, observations, "")),
            color = "black", size = 3) +
  coord_equal() +
  scale_x_continuous(limits = c(1968, 2010), expand = c(0, 0), breaks = seq(1969, 2009, 1)) +
  labs(fill = "Country-Year Study Count")

dev.off()

